package com.itheima.controller;


import com.itheima.common.R;
import com.itheima.domain.Admin;
import com.itheima.domain.Role;
import com.itheima.manager.UmsAdminManager;
import com.itheima.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.List;
import java.util.Map;

@RestController
public class UmsAdminController {
    @Autowired
    UmsAdminManager umsAdminManager;
    //登录
    @PostMapping("/admin/login")
    public R<Map<String,String>> login(@RequestBody Map<String,String> map){
        return umsAdminManager.login(map);
    }

    //查询当前登录用户信息，包括对应角色和菜单
    @GetMapping("/admin/info")
    public R<Map<String,Object>> findAdminInfo(@RequestParam(required = false) String name){
        return umsAdminManager.findAdminInfo(name);
    }
    //退出登录
    @PostMapping("/admin/logout")
    public R<String> logout(){
        return umsAdminManager.logout();
    }

    //根据条件分页查询用户
    @GetMapping("/admin/list")
    public R<PageBeanVo> findByPageAndKeyword(
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value="pageSize",defaultValue = "10")Integer pageSize,
            @RequestParam(value="keyword",required = false) String keyword){
        return umsAdminManager.findByPageAndKeyword(pageNum,pageSize,keyword);
    }

    //注册
    @PostMapping("/admin/register")
    public R<String> register(@RequestBody Admin admin){
        return umsAdminManager.register(admin);
    }

    //根据adminId查询对应角色
    @GetMapping("/admin/role/{adminId}")
    public R<List<Role>> findRoleByAdminId(@PathVariable Integer adminId){
        return umsAdminManager.findRoleByAdminId(adminId);
    }

    //根据id删除指定用户
    @PostMapping("/admin/delete/{id}")
    public R<String> deleteById(@PathVariable Long id){
        return umsAdminManager.deleteById(id);
    }

    //给用户分配角色
    @PostMapping("/admin/role/update")
    public R<String> updateRole(String adminId,String roleIds){

        return umsAdminManager.updateRole(adminId,roleIds);
    }

    //根据id修改用户信息
    @PostMapping("/admin/update/{id}")
    public R<String> updateById(@PathVariable Long id,@RequestBody Admin admin){
        return umsAdminManager.updateById(admin);
    }

    //根据id修改用户状态
    @PostMapping("/admin/updateStatus/{id}")
    public R<String> updateStatus(@PathVariable Long id,Integer status){
        return umsAdminManager.updateStatus(id,status);
    }

    //根据id获取指定用户信息
    @GetMapping("/admin/{id}")
    public R<Admin> findById(@PathVariable Long id){
        return umsAdminManager.findById(id);
    }

    //修改指定用户密码
    @PostMapping("/admin/updatePassword")
    public R<String> updatePassword(@RequestBody Map<String,String> map){
        String oldPassword = map.get("oldPassword");
        String newPassword = map.get("newPassword");
        String username = map.get("username");
        return umsAdminManager.updatePassword(oldPassword,newPassword,username);
    }

    //刷新token
    @GetMapping("/admin/refreshToken")
    public R<Map<String,String>> refreshToken(){
        return umsAdminManager.refreshToken();
    }
}
